package uk.ac.lkl.lpp.model.xml;



//import java.util.HashMap;
import java.util.Map;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import uk.ac.lkl.common.util.ds.AbstractNotifyingList;
import uk.ac.lkl.common.util.ds.NotifyingMap;
import uk.ac.lkl.lpp.model.PedagogyPlanModel;

import uk.ac.lkl.lpp.model.ldo.TeachingMethod;



public class TeachingMethodGroupSizeSetProcessor extends ModelElementProcessor {

    public TeachingMethodGroupSizeSetProcessor() {
        super("TeachingMethodGroupSizeSet");
    }


    public void convertDataToElements(PedagogyPlanModel plan,
                                      Document document,
                                      Element pedagogyPlanElement) {

        // should be done in base-class
        Element hoursElement = document.createElement("TeachingMethodGroupSizeSet");
        pedagogyPlanElement.appendChild(hoursElement);

        // hack to use this to look up indices all the time
        AbstractNotifyingList<TeachingMethod> teachingMethods = plan.getList("main", TeachingMethod.class);

        Map<TeachingMethod, Integer> map = plan.getMap("TeachingMethodGroupSize",
                                                       TeachingMethod.class,
                                                       Integer.class);
        for (Map.Entry<TeachingMethod, Integer> entry : map.entrySet()) {
            TeachingMethod teachingMethod = entry.getKey();
            Integer groupSize = entry.getValue();

            // user didn't specify it
            if (groupSize == null)
                continue;

            int index = teachingMethods.indexOf(teachingMethod);

            Element entryElement = document.createElement("TeachingMethodGroupSize");
            entryElement.setAttribute("teachingMethodIndex",
                                      Integer.toString(index));
            entryElement.setAttribute("groupSize", Integer.toString(groupSize));

            hoursElement.appendChild(entryElement);
        }
    }


    public void convertElementToData(PedagogyPlanModel plan, Element element) {
        NotifyingMap<TeachingMethod, Integer> map = new NotifyingMap<TeachingMethod, Integer>();

        AbstractNotifyingList<TeachingMethod> teachingMethods = plan.getList("main", TeachingMethod.class);

        NodeList nodes = element.getElementsByTagName("TeachingMethodGroupSize");
        for (int i = 0; i < nodes.getLength(); i++) {
            try {
                Element childElement = (Element) nodes.item(i);

                String indexString = childElement.getAttribute("teachingMethodIndex");
                int index = Integer.parseInt(indexString);
                TeachingMethod teachingMethod = teachingMethods.get(index);

                String groupSizeString = childElement.getAttribute("groupSize");
                Integer groupSize = Integer.parseInt(groupSizeString);

                map.put(teachingMethod, groupSize);
            }
            catch (NumberFormatException e) {
                // todo: add to warning list
                e.printStackTrace();
            }
        }
        plan.addMap("TeachingMethodGroupSize",
                    TeachingMethod.class,
                    Integer.class,
                    map);
    }

}
